using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Reflection;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
using System.Net;
using System.Threading;


namespace SMTPdll
{
    public class SMTPSender
    {
        Socket s;
        System.IO.StreamReader connectionRead;
        string to;
        string from;
        string sub;
        string body;
        public string resultLines;
        string filename;
        string fileend;
        string boundary = "JOHNkurtJERADjohnKURTJeradPOP3C#ROXORS";
        bool attachSomething;

        public SMTPSender(string host, bool hasAttach)
        {
            s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Connect(host, 25);
            connectionRead = new System.IO.StreamReader(new NetworkStream(s));
            attachSomething = hasAttach;
        }

        public bool sendMessage()
        {
            string output;

            resultLines += connectionRead.ReadLine();
            s.Send(System.Text.Encoding.UTF8.GetBytes("HELO\r\n"));


            resultLines += "HELO\r\n";
            output = connectionRead.ReadLine();
            resultLines += output;
            resultLines += "\r\n";
            if (!output.StartsWith("250"))
            {
                return false;
            }

            s.Send(System.Text.Encoding.UTF8.GetBytes("MAIL FROM: <" + from + ">\r\n"));
            resultLines += "MAIL FROM: <" + from + ">\r\n";
            output = connectionRead.ReadLine();
            resultLines += output;
            resultLines += "\r\n";
            if (!output.StartsWith("250"))
            {
                return false;
            }

            s.Send(System.Text.Encoding.UTF8.GetBytes("RCPT TO: <" + to + ">\r\n"));
            resultLines += "RCPT TO: <" + to + ">\r\n";
            output = connectionRead.ReadLine();
            resultLines += output;
            resultLines += "\r\n";
            if (!output.StartsWith("250"))
            {
                return false;
            }

            s.Send(System.Text.Encoding.UTF8.GetBytes("DATA\r\n"));
            resultLines += "DATA\r\n";
            output = connectionRead.ReadLine();
            resultLines += output;
            resultLines += "\r\n";
            if (!output.StartsWith("354"))
            {
                return false;
            }
            s.Send(System.Text.Encoding.UTF8.GetBytes("From: " + from + "\r\n"));
            s.Send(System.Text.Encoding.UTF8.GetBytes("Subject: " + sub + "\r\n"));
            s.Send(System.Text.Encoding.UTF8.GetBytes("Mime-Version: 1.0\r\n"));
            s.Send(System.Text.Encoding.UTF8.GetBytes("Content-Type: multipart/mixed;\r\n"));
            s.Send(System.Text.Encoding.UTF8.GetBytes("\tboundary="+ boundary +"\r\n\r\n"));

            s.Send(System.Text.Encoding.UTF8.GetBytes("--"+ boundary + "\r\n"));
            s.Send(System.Text.Encoding.UTF8.GetBytes("Content-Type: text/plain; \r\n"));
            s.Send(System.Text.Encoding.UTF8.GetBytes("Content-Transfer-Encoding: 8bit \r\n\r\n"));
            s.Send(System.Text.Encoding.UTF8.GetBytes(body + "\r\n"));

            if (attachSomething)
            {
                char[] splitchar = { '\\' };
                string[] fileparts = new string[] { "" };
                fileparts = filename.Split(splitchar);
                fileend = fileparts[fileparts.Length - 1];
                s.Send(System.Text.Encoding.UTF8.GetBytes("--"+boundary +"\r\n"));
                s.Send(System.Text.Encoding.UTF8.GetBytes("Content-Type: application/octet-stream; name=\"" + fileend + "\"\r\n"));
                s.Send(System.Text.Encoding.UTF8.GetBytes("Content-Transfer-Encoding: base64\r\n"));
                s.Send(System.Text.Encoding.UTF8.GetBytes("Content-Description: " + fileend + "\r\n"));
                s.Send(System.Text.Encoding.UTF8.GetBytes("Content-Disposition: attachment; filename=\"" + fileend + "\"\r\n\r\n"));
                s.Send(System.Text.Encoding.UTF8.GetBytes(encodeFileToString(filename) + "\r\n"));
                s.Send(System.Text.Encoding.UTF8.GetBytes("--"+boundary +"\r\n.\r\n"));
            }
            else {
                s.Send(System.Text.Encoding.UTF8.GetBytes("--"+ boundary +"\r\n.\r\n"));
            
            }


            resultLines += "Subject: " + sub + "\r\n" + "From: " + from + "\r\n\r\n" + body + "\r\n.\r\n";
            output = connectionRead.ReadLine();
            resultLines += output;
            resultLines += "\r\n";
            if (!output.StartsWith("250"))
            {
                return false;
            }

            s.Send(System.Text.Encoding.UTF8.GetBytes("QUIT\r\n"));
            resultLines += "QUIT\r\n";
            resultLines += connectionRead.ReadLine();

            //connectionRead.Dispose();
            //s.Disconnect(true);

            return true;
        }

        public bool setFromAddress(string a)
        {
            from = a;
            if (from == a)
                return true;
            else
                return false;
        }

        public bool setToAddress(string a)
        {
            to = a;
            if (to == a)
                return true;
            else
                return false;
        }

        public bool setSubject(string a)
        {
            sub = a;
            if (sub == a)
                return true;
            else
                return false;
        }

        public bool setBody(string a)
        {
            body = a;
            if (body == a)
                return true;
            else
                return false;
        }

        public string encodeFileToString(string filepath)
        {
            //using System.IO;
            byte[] array = File.ReadAllBytes(filepath);
            return Convert.ToBase64String(array);
        }

        public void setfilename(string s)
        {
            filename = s;
            return;
        }

    }
}

